package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    //查询
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //前登录用户ID查询购物车
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    //新增
    @PostMapping("/add")
    public R<ShoppingCart> addshop(@RequestBody ShoppingCart shoppingCart){
        //获取当前登录的用户
        Long currentId = BaseContext.getCurrentId();
        //获取购物车的用户
        shoppingCart.setUserId(currentId);
        //查询放入数据库购物车用户的id
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,currentId);
        //获取加入购物车的菜品id
        Long dishId = shoppingCart.getDishId();
        //如果传入购物车的菜品不为空放入数据库里
        if (dishId!=null){
            wrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //如果为空就放的是套餐id
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        //如果不存在，则添加到购物车，数量默认就是一
        if (one==null){
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
            //如果有就修改购物车里的数量
        }else {
            Integer number = one.getNumber();
            one.setNumber(number+1);
            shoppingCartService.updateById(one);
        }

        return R.success(one);
    }


    //取消购物车
    @PostMapping("/sub")
    public R<ShoppingCart> subshop(@RequestBody ShoppingCart shoppingCart){
        //获取当前登录的用户名
        Long userId = BaseContext.getCurrentId();
        //登录的用户名查询购物车用户名
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        //获取菜品id判断是 菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){
            wrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        //   number -1
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        //如果    数量大于0修改进行递减
        if (number>0){
            shoppingCartService.updateById(one);
        }else {
            //如果 小于等于0直接删除菜品或者套餐
            shoppingCartService.remove(wrapper);
        }
        return R.success(one);
    }


    //清空购物车
    @DeleteMapping("/clean")
    public R<String> removeshop(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //获取购物车的用户删除
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(wrapper);
        return R.success("清空成功！");
    }

}
